const express = require('express')
const path = require('path') //系统路径模块
const fs = require('fs') //文件模块
const bodyParser = require('body-parser')

const app = express()

//使用body-parser中间件，获取post请求数据
app.use(bodyParser.urlencoded({ limit: '50mb', extended: false }))
app.use(bodyParser.json({ limit: '50mb' }))

// 解决跨域问题
app.all("/*", function (req, res, next) {
  // 跨域处理
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
  res.header("X-Powered-By", ' 3.2.1');
  res.header("Content-Type", "application/json;charset=utf-8");
  next(); // 执行下一个路由
})

app.get('/register', function (req, res) {

  let file = path.join(__dirname, 'pic.json')

  fs.readFile(file, 'utf-8', function (err, data) {
    if (err) {
      res.send('读取文件失败')
    } else {
      res.send(data)
    }
  })
})

app.post('/check', function (req, res) {

  console.log(req.body);

  let sliceX = req.body.sliceX
  let diff = parseInt(Math.abs(sliceX - 73))
  parseInt
  console.log(diff);
  if (diff < 5) {
    res.send({ "code": 200 })
  } else {
    res.send({ "code": 201 })
  }


})

app.listen(5000, () => {
  console.log('server is running on 5000...')
})